<?php

namespace App\Admin\Controllers;

use App\Admin\Extension\Expand\GoodsGroupValue;
use App\Admin\Repositories\TongGoodsGroup;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class TongGoodsGroupController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new TongGoodsGroup(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->model()->orderBy('id', 'desc');
            $grid->column('goods_group_name');
            $grid->column('goods_group_price');
            $grid->column('goods_group_img')->display(function($value){
                return explode(',',$value);
            })->gallery();
            $grid->column('goods_status','上架')->switch();
            $grid->column('value', '属性值')
                ->display('查看')
                ->expand(GoodsGroupValue::class);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id')->placeholder('请输入商品id');
                $filter->like('goods_group_name', '名称')->placeholder('请输入商品名称');
                $filter->equal('goods_status','上架状态')->select([0=>'下架',1=>'上架'])->placeholder('请选择商品上下架');
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableEdit();
                $actions->disableQuickEdit();
                $actions->disableView();
            });

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new TongGoodsGroup(), function (Show $show) {
            $show->field('id');
            $show->field('goods_group_name');
            $show->field('goods_group_price');
            $show->field('goods_group_img');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new TongGoodsGroup(), function (Form $form) {
            $form->display('id');
            $form->text('goods_group_name','名称')->rules('required', [
                'required' => '请输入套餐名称',
            ]);
            $form->currency('goods_group_price','价格')->rules('required', [
                'required' => '请输入套餐价格',
            ]);
            $form->radio('goods_status','上架')->options([0 => '下架', 1=> '上架'])->default(0);
            $form->image('goods_group_img','套餐图片')->autoUpload()->rules('required', [
                'required' => '请上传套餐图片',
            ]);
            $form->hasMany('values', '属性值', function (Form\NestedForm $table) {
                $table->text('name', '名称')->help('属性的值（例如：套餐一，套餐二，套餐三）');
                $table->image('image', '图片')->autoUpload();
            })->useTable();

            $form->footer(function ($footer) {
                // 去掉`查看`checkbox
                $footer->disableViewCheck();

                // 去掉`继续编辑`checkbox
                $footer->disableEditingCheck();

                // 去掉`继续创建`checkbox
                $footer->disableCreatingCheck();
                $footer->disableViewCheck();


            });

            $form->disableResetButton();
        });
    }
}
